package fallingFlower;

import java.util.ArrayList;
import java.util.Random;
import java.util.function.Consumer;
import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.RotateTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
import media.MediaPlayerDemo;

// 落花( 缩放(3D效置)和位移(动画) )
public class FallingFlower extends Application {
	private Pane root = new Pane();
	private SubScene ss;			// 子场景
	private Pane pane = new Pane();		// 子场景面板
	private ImageView backgroundImage;		// 背景图片
	private ArrayList<ImageView> flowers = new ArrayList<>();	// 花朵图片数组
	private static final double ROOTPANE_WIDTH = 1000;		// 根面板的宽高
	private static final double ROOTPANE_HEIGHT= 650;
	private Random random =  new Random();
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		// 背景图片
		backgroundImage = new ImageView("image/6.jpg");
		backgroundImage.setFitWidth(ROOTPANE_WIDTH);
		backgroundImage.setFitHeight(ROOTPANE_HEIGHT);
		
		// 加入花朵图片
		for(int i=0; i<35; i++) {
			ImageView flower = new ImageView("image/红色花朵.png");
			flower.setTranslateX(random.nextDouble()*ROOTPANE_WIDTH - 150);
			flower.setTranslateY(random.nextInt(20));	
			flower.setTranslateZ(random.nextInt(2500));
			flowers.add(flower);
		}
		pane.getChildren().addAll(flowers);
		
		ss = new SubScene(pane, ROOTPANE_WIDTH, ROOTPANE_HEIGHT, true, SceneAntialiasing.BALANCED);
		ss.setCamera(new PerspectiveCamera());
		root.getChildren().addAll(backgroundImage, ss);
		  
	    Scene scene = new Scene(root, ROOTPANE_WIDTH, ROOTPANE_HEIGHT);
	    stage.setScene(scene);
	    stage.getIcons().add(new Image("image/左上方图标.jpg"));
		stage.show();
		
		// 事件
		// 播放音乐
		MediaPlayerDemo.playMusic("mother.mp3");
		
		// 落花的位移和旋转和褪色(可在图片数组或面板中遍历)
		flowers.forEach(new Consumer<ImageView>() {

			@Override
			public void accept(ImageView t) {
				double time = random.nextDouble()*10 + 6;
				TranslateTransition translate = new TranslateTransition(Duration.seconds(time), t);
				translate.setByX(400);
				translate.setByY(700);
				translate.setCycleCount(Timeline.INDEFINITE);
				
				RotateTransition rotate = new RotateTransition(Duration.seconds(time), t);
				if(time < 10) {
					rotate.setAxis(Rotate.Z_AXIS);
				} else if(time < 13) {
					rotate.setAxis(Rotate.X_AXIS);
				} else {
					rotate.setAxis(Rotate.Y_AXIS);
				}
				rotate.setFromAngle(0);
				rotate.setToAngle(720);
				rotate.setCycleCount(Timeline.INDEFINITE);
				
				FadeTransition fade1 = new FadeTransition(Duration.seconds(time/2), t);
				fade1.setFromValue(0);
				fade1.setToValue(1);
				
				FadeTransition fade2 = new FadeTransition(Duration.seconds(time/2), t);
				fade2.setFromValue(1);
				fade2.setToValue(0.3);
				
				SequentialTransition sequential = new SequentialTransition(fade1, fade2);
				sequential.setCycleCount(Timeline.INDEFINITE);
				
				ParallelTransition parallel = new ParallelTransition(translate, rotate, sequential);
				parallel.play();
			}
		});
	}
}
